<?php
/**
 * Created by PhpStorm.
 * User: dreamy
 * Date: 2019-07-05
 * Time: 16:34
 */
namespace common\services;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

/**
 * Class UploadService
 * @package common\services
 * 上传服务
 */
class UploadService extends BaseService
{

    /**
     * @param $filePath string 文件路径
     * @param $uploadKey string 文件存储名称
     * @throws
     * @return boolean
     * 上传文件 调用七牛云
     */
    public static function putFile($filePath,$uploadKey)
    {
        $accessKey  = getenv('QINIU_ACCESS_KEY');
        $secretKey  = getenv('QINIU_SECRET_KEY');
        $bucket     = getenv('QINIU_IMG_BUCKET');
        $auth       = new Auth($accessKey, $secretKey);
        $uploadMgr  = new UploadManager();
        $token      = $auth->uploadToken($bucket);
        list($ret, $err) = $uploadMgr->putFile($token, $uploadKey, $filePath);
        if ($err !== null) {
            self::setError($err);
            return false ;
        } else {
            return $ret;
        }
    }
}